<!-- 角色管理-->
<div id="rolePage" class="layui-row">
    <div class="layui-col-md12">
        <div class="layui-form layui-table-filter">
    
        <table id="roleTable" class="layui-table" lay-filter="roleTable"></table>
    </div>
    <div class="layui-col-md12" style="margin: 28px 0 0 0;"></div>
</div>
<script type="text/html" id="toolbarRole">
    <div class="layui-btn-container">
      <button class="layui-btn layui-btn-sm" lay-event="addRole">添加角色</button>
      <button class="layui-btn layui-btn-sm" lay-event="trashRole">批量删除</button>
    </div>
</script>
<script type="text/html" id="barRole">
    <a class="layui-btn layui-btn-xs" lay-event="menu">菜单权限</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="isDefaultTpl">
    {{#  if(d.isDefault === 1){ }}
      <span style="color: #67dd5c;">是</span>
    {{#  } else { }}
    <span style="color: #ca430e;">否</span>
    {{#  } }}
</script>

<script>
    layui.use(['table', 'base','index'], function () {
        var $ = layui.jquery,
        table = layui.table,
        layer = layui.layer,
        util = layui.util,
        base = layui.base;
        //用户表格
        var roleTable = table.render({
            elem: '#roleTable',
            url: '/baseService/role/layTable',
            method: 'post',
            contentType: 'application/json',
            toolbar: '#toolbarRole', //开启头部工具栏，并为其绑定左侧模板
            // headers: config.getHeaders(),
            where: {
                 orderColumn: 'createTime',
                 orderDir: 'asc',
            },
            page: {
                layout: ['prev', 'page', 'next', 'skip', 'count', 'limit', 'refresh'], //自定义分页布局
                curr: 1, //设定初始在第 5 页
                groups: 3, //只显示 1 个连续页码
                limit: 10,
                limits: [5,10,20,50],
                first: false, //不显示首页
                last: false //不显示尾页
            },
            cols: [[
                {type:'checkbox'},
                {field: 'id', hide: true},
                {field: 'roleCode', title: '角色标识', sort: true},
                {field: 'roleName', title: '角色名称', sort: true},
                {field: 'isDefault', templet: '#isDefaultTpl', title: '是否默认角色'},
                {field: 'createTime', title: '创建时间', sort: true},
                {field: 'updateTime', title: '更新时间', sort: true},
                {fixed: 'right', align: 'center', toolbar: '#barRole', fixed:'right', title: '操作',width: 220}
            ]],
            response: {
                statusCode: 200 //规定成功的状态码，默认：0
            },
            done: function (res, curr, count) {
            }
        });

        //头工具栏事件
        table.on('toolbar(roleTable)', function(obj){
            switch(obj.event){
                case 'addRole':       //添加角色
                base.removeSessionData('fs_role');
                base.open({
                    title: "添加角色",
                    id:"addRoleLayer",
                    path: 'roleAdd.html',
                    area: '360px',
                    finish: function () {
                        table.reload('roleTable');
                    }   
                });
                break;
                case 'trashRole':       //删除角色
                var checkStatus = table.checkStatus(obj.config.id).data;
                var ids=[], names=[];
                if (checkStatus.length < 1) {
					layer.alert("请至少选择一条数据！");
					return false;
				}
				checkStatus.forEach(function(item, index) {
					ids.push(item.id);
                    names.push(item.roleCode);
				})
                layer.confirm("确认要删除角色："+names.toString()+"？", {icon: 3, title:'提示'}, function(index){
                    base.fsAjax('/baseService/role/batchDel'+ids.toString(), {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 200) {
                            table.reload('roleTable');
                            base.msgSuccess(data.msg);
                        } else {
                            base.msgError(data.msg);
                        }
                    }, 'POST');
                });
                break;
            };
        });

        //监听行工具事件
        table.on('tool(roleTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确认要删除角色：'+data.roleCode+'？', function(index){
                    base.fsAjax('/baseService/role/logicalDel/'+data.id, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 200) {
                            table.reload('roleTable');
                            base.msgSuccess(data.msg);
                        } else {
                            base.msgError(data.msg);
                        }
                    }, 'POST');
                });
            } else if(obj.event === 'edit'){
                base.putSessionData('fs_role', data);
                base.open({
                    title: "编辑角色",
                    id:"editRoleLayer",
                    path: 'roleEdit.html',
                    finish: function () {
                        base.removeSessionData('fs_role');
                        table.reload('roleTable');
                    }   
                });
            }
        });

    });
</script>